import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import { routerRole } from '../api/fun'
// import { useStore } from 'vuex';
import store from "../store";
import Layout from '@/layout/index.vue'

// const $store = useStore()
const routes: Array<RouteRecordRaw> = [
  {
    path: '/', redirect: '/home',name: 'init',component:Layout,children: [
        { path: '/home', name:'putong', meta: {title: '首页',icon:'MailOutlined'},component: () => import('../view/putong.vue') },
        { path: '/index',name:'index', meta: {title: '测试'}, component:() => import('../view/index.vue') },
        { path: '/zujian',name:'zujian',meta: {title: '菜单'}, component: () => import('../view/router-view.vue'),children:[{
          path: 'zujian123',name:'haha',meta: {title: '菜单'}, component: () => import('../view/router-view.vue'),children: [{
            path: 'admin123' , name:'admin1', meta: {title: '菜单3'}, component: () =>  import('../view/admin.vue')
          }]
        }] }
    ]
  },
  {path: '/login',name: 'login',component: () => import('@/view/login/login')}
];

// [
//   {
//     path: '/', redirect: '/home',name: 'init'
//   },
//   { path: '/home', name:'putong', meta: {title: '首页'},component: Layout,
//         { path: '/index',name:'index', meta: {title: '测试'}, component:() => import('../view/index.vue') },
//         { path: '/zujian',name:'zujian',meta: {title: '菜单'}, component: () => import('../view/router-view.vue'),children:[{
//           path: 'zujian123',name:'haha',meta: {title: '菜单'}, component: () => import('../view/router-view.vue'),children: [{
//             path: 'admin123' , name:'admin1', meta: {title: '菜单3'}, component: () =>  import('../view/admin.vue')
//           }]
//         }] }
// ];

const role = 'admin'
const adminRouter  = [
  { path: 'admin' , name:'admin', meta: {title: '菜单一'}, component: () =>  import('../view/admin.vue')},
  {path: 'nothing', name: 'nothing', meta: {title: '菜单二'}, component: () => import('../view/nothing.vue')}]
const endrouter = [{
  path: '/:pathMatch(.*)*',name:'404', hidden: true, component: () => import('../view/404.vue')
}]
// if(role === 'admin') {
//   routerRole(adminRouter,router,'zujian')
//   routerRole(endrouter,router, 'zujian')
// } else {
//   routerRole(endrouter,router, 'zujian')
// }
if(role === 'admin') {
  routerRole(adminRouter,routes,'zujian')
  routerRole(endrouter,routes, 'zujian')
} else {
  routerRole(endrouter,routes, 'zujian')
}

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// router.beforeEach( (to, from, next) => {
//   const state = store.state.user
//   if(to.path !== '/login' && !state) next('/login') 
//   else next()
// })
router.beforeEach(  (to,from) => {
  const state = store.state.user
  if(to.path !== '/login' && !state) {
    return {
      name: 'login'
    }
  }
  return 
})

// console.log(router.hasRoute('admin'));
// console.log(router.getRoutes())
router.onError(info => {
  console.log("error", info);
} )

export default router;
